Site logo

Override all pageItems function

While overriding page items on a master spread who has more than one page, it’s important to check if the current item is located on the same side as the destination page. Otherwise you will end up with an error: invalid object for this request.

var doc = app.activeDocument;
overrideAllPageItems();

function overrideAllPageItems() {
	var page, allPageItems, pageItem;
	
	for (var c = 0; c < doc.pages.length; c++) {
		page = doc.pages[c];
		allPageItems = page.appliedMaster.allPageItems;
		
		for (var d = allPageItems.length - 1; d >= 0; d--) {
			pageItem = allPageItems[d];
			
			if (pageItem.parentPage.side == page.side) {
				pageItem.override(page);
			}
		}
	}
}

Alternatively, you can enclose override command with try-catch block to silently skip all problematic items.

try {
	pageItem.override(page);
} catch (err) {}

Also, if the menu command "Override All Master Page Items" works for you and you do not use InDesign Server, but the desktop version, you should be able to invoke the menu command by scripting like so:

var mA = app.menuActions.itemByName("$ID/Override All Master Page Items");
if( mA.isValid && mA.enabled ){ mA.invoke() };

See also the override scripts section